const express = require('express')
const router = express.Router()
const multipart = require('../../middleware/content-type/multypart')
const { insertComments, getCommenListByProid, getCommenListByUserid } = require('../../model/mongo')

router.use(multipart('./server/upload'))

router.post('/add', async (req, res) => {
    const { proid, userid, fileList, star, content, imgs, orderid } = req.body
    const body = {
        proid,
        userid,
        imgs: fileList && fileList.map(item => item.path) || imgs || [],
        star: Number(star),
        content,
        orderid
    }
    const result = {
        code: 200,
        msg: '添加商品评价'
    }
    result.msg = await insertComments(body)
    res.send(JSON.stringify(result))
})

router.get('/list', async (req, res) => {
    const { proid } = req.query
    const result = {
        code: 200,
        msg: '获取商品评价'
    }
    result.data = await getCommenListByProid(proid)
    res.send(JSON.stringify(result))
})

router.get('/my', async (req, res) => {
    const { userid } = req.query
    const result = {
        code: 200,
        msg: '获取我的评价',
    }
    result.data = await getCommenListByUserid(userid)
    res.send(JSON.stringify(result))
})
module.exports = router
